package com.csu.mypetstore.api.controller.front;

import com.csu.mypetstore.api.common.CommonResponse;
import com.csu.mypetstore.api.common.ResponseCode;
import com.csu.mypetstore.api.entity.Account;
import com.csu.mypetstore.api.entity.Cartmessage;
import com.csu.mypetstore.api.entity.Item;
import com.csu.mypetstore.api.persistence.AccountMapper;
import com.csu.mypetstore.api.persistence.CartmessageMapper;
import com.csu.mypetstore.api.service.AccountService;
import com.csu.mypetstore.api.service.CartService;
import com.csu.mypetstore.api.service.CatalogService;
import com.csu.mypetstore.api.vo.AccountVO;
import com.csu.mypetstore.api.vo.CartVO;
import com.csu.mypetstore.api.vo.CartmessageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/cart/")
public class CartController {

    @Autowired
    private CartService cartService;

    @Autowired
    private CatalogService catalogService;

    @Autowired
    private AccountMapper accountMapper;


    //通过username查看购物车
    @GetMapping("cartItems/{username}")
    @ResponseBody
    public CommonResponse<List<CartmessageVO>> getCartByUsername(
            @PathVariable("username") String username,
            HttpSession session){
        Account accountVO = accountMapper.selectById(username);
//        AccountVO loginAccount = (AccountVO) session.getAttribute("login_account");
        if(accountVO!=null){
            CommonResponse<List<CartmessageVO>> response = cartService.getCartByUsername(username);
            if(response.isSuccess()){
                session.getAttribute("cart");
            }
            return response;
        }
        return CommonResponse.createForError(ResponseCode.NEED_LOGIN.getCode(), "用户未登录");
    }


    //插入购物车
    @PostMapping("insertCart/{username}/{ItemId}")
    @ResponseBody
    public CommonResponse<Boolean> insertCart(
            @PathVariable("ItemId") String ItemId,
            @PathVariable("username") String username,
            HttpSession session){
        CartmessageVO cartmessageVO = new CartmessageVO();
        Account account = accountMapper.selectById(username);
        if(ItemId != null){
            CartVO cart = (CartVO) session.getAttribute("cart");

            if(cart == null || cart.getNumberOfItems() == 0){//为空新建购物车
                cart = new CartVO();
            }

            if(cart.containsItemId(ItemId)){
                cart.incrementQuantityByItemId(ItemId);
                cartmessageVO = cart.getCartItemById(ItemId);
                int number =cartmessageVO.getQuantity();
                cartmessageVO.setUsername(account.getUsername());
                session.setAttribute("cart",cart);
                return cartService.updateCart(cartmessageVO);
            }else{
                boolean isInStock = catalogService.isItemInStock(ItemId);
                Item item = catalogService.getItem(ItemId);
                cart.addItem(item,isInStock);
                cartmessageVO = cart.getCartItemById(ItemId);
                cartmessageVO.setUsername(account.getUsername());
                cartmessageVO.setItemId(ItemId);
                cartmessageVO.setQuantity(1);

            }
        }
        return cartService.insertCart(cartmessageVO);
    }

    //更新购物车
    @PostMapping("updateCart/{cartId}/{quantity}")
    @ResponseBody
    public CommonResponse<Boolean> updateCart(
            @PathVariable("cartId") String cartId,
            @PathVariable("quantity")int quantity,
            HttpSession session){

        if(cartId!=null){
            Cartmessage cartmessage = cartService.selectCartItemByCartId(cartId);
            Item item = cartService.selectItemById(cartmessage.getItemId());
            CartmessageVO cartmessageVO = cartService.cartmessageToCartmessageVO(cartmessage,item);
            cartmessageVO.setQuantity(quantity);
            if(quantity<1){
                return cartService.removeCart(cartmessageVO);
            }
            else
                return cartService.updateCart(cartmessageVO);
        }
        return CommonResponse.createForError(ResponseCode.NEED_LOGIN.getCode(), "更新失败");

    }

    //删除购物车
    @GetMapping("removeCart/{username}/{ItemId}")
    @ResponseBody
    public CommonResponse<Boolean> removeCart(
                                    @PathVariable("ItemId") String ItemId,
                                    @PathVariable("username") String username,
                                    HttpSession session){
        CartVO cart = (CartVO)session.getAttribute("cart");
        Account account = accountMapper.selectById(username);
        CartmessageVO cartItem = new CartmessageVO();
        cartItem.setItemId(ItemId);
        cartItem.setUsername(account.getUsername());

        return cartService.removeCart(cartItem);
    }


        //删除购物车
    @GetMapping("removeCartByCartID/{CartId}")
    @ResponseBody
    public CommonResponse<Boolean> removeCartByCartId(@PathVariable("CartId") String CartId,
                                                        HttpSession session){

        if(CartId!=null)
            return cartService.removeCartByCartId(CartId);
        else
            return CommonResponse.createForError(ResponseCode.NEED_LOGIN.getCode(), "删除失败");
    }
}
